package com.matt.service.impl;

import com.aliyun.openservices.ons.api.Message;
import com.matt.domain.MQMessage;
import com.matt.repository.MQMessageRepository;
import com.matt.service.MQMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/12/27
 * *******************************
 */
@Service
public class MQMessageServiceImpl implements MQMessageService {

    @Autowired
    private MQMessageRepository mqMessageRepository;


    @Override
    public Boolean saveAndRefresh(Message message, Integer status) {
        MQMessage mqMessage = mqMessageRepository.findByMsgId(message.getMsgID());
        if (Objects.isNull(mqMessage))
            mqMessage = MQMessage.of(message, status);
        else {
            mqMessage.setConsumerTimes(mqMessage.getConsumerTimes() + 1);
            mqMessage.setStatus(status);
            mqMessage.setLastConsumed(new Date());
        }
        mqMessageRepository.save(mqMessage);
        return true;
    }
}
